﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using sharpCmd.Classes;
using sharpCmd.Classes.IO;

namespace sharpCmd.Dialogs.PanelActions
{
    public partial class CopyForm : Form
    {
        public CopyForm()
        {
            InitializeComponent();
            progressBarWithText1.Maximum = 100;
        }

        public IEnumerable<FSObjectInfo> Infos { get; set; }
        public string TargetDirectory { get; set; }

        private void CopyForm_Load(object sender, EventArgs e)
        {
            if (Infos == null || TargetDirectory == null)
                throw new InvalidOperationException("CopyForm: Infos or TargetDirectory is null!");
        }

        private void CopyForm_Shown(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Close();
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            labelCurrentFile.Text = e.UserState.ToString(); //TODO show as much of the file name as fits, but backwards (e.g. ...ram Files\Internet Explorer\iexplore.exe)
            progressBarWithText1.Value = e.ProgressPercentage;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            int i = 0;
            foreach (var info in Infos)
            {
                //for now copy only files
                if (info.Type == FSObjectType.Directory)
                    continue;
                ((BackgroundWorker)sender).ReportProgress((int)((float)i / (float)Infos.Count() * 100), info.Name);
                i++;
                File.Copy(info.FullName, Path.Combine(TargetDirectory, info.Name + "." + info.Extension));
            }
        }
    }
}
